package cn.hzking.gjmall.adpters.car;


import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import cn.hzking.gjmall.R;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.bean.car.entity.CarBean;
import cn.hzking.gjmall.utils.IconTextSpanUtils;
import cn.hzking.gjmall.utils.PriceFormatUtils;
import cn.hzking.gjmall.utils.ScreenTool;
import cn.hzking.gjmall.utils.StringUtils;


public class ShopcartExpandableListViewAdapter extends BaseExpandableListAdapter {
    private ArrayList<CarBean.DataBean.EffectBean.ActivityBean> groups = new ArrayList<>();//组元素
    private HashMap<Integer, List<CarBean.DataBean.EffectBean.ActItemsBean>> children = new HashMap<>();//子元素
    private Context context;
    private CheckInterface checkInterface;
    private ModifyCountInterface modifyCountInterface;

    /**
     * 构造函数
     *
     * @param groups   组元素列表
     * @param children 子元素列表
     * @param context
     */
    public ShopcartExpandableListViewAdapter(ArrayList<CarBean.DataBean.EffectBean.ActivityBean> groups, HashMap<Integer, List<CarBean.DataBean.EffectBean.ActItemsBean>> children, Context context) {
        super();
        this.groups = groups;
        this.children = children;
        this.context = context;
    }

    public ShopcartExpandableListViewAdapter(Context context) {
        super();
        this.context = context;
    }

    public void setNewData(ArrayList<CarBean.DataBean.EffectBean.ActivityBean> mgroups, HashMap<Integer, List<CarBean.DataBean.EffectBean.ActItemsBean>> mchildren) {
        groups.clear();
        children.clear();
        this.groups.addAll(mgroups);
        this.children.putAll(mchildren);
        notifyDataSetChanged();
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;
    }

    public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
        this.modifyCountInterface = modifyCountInterface;
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int groupId = groups.get(groupPosition).getmId();
        return children.get(groupId).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<CarBean.DataBean.EffectBean.ActItemsBean> childs = children.get(groups.get(groupPosition).getmId());
        return childs.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        CarBean.DataBean.EffectBean.ActivityBean group = (CarBean.DataBean.EffectBean.ActivityBean) getGroup(groupPosition);
        return group.getmId();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        CarBean.DataBean.EffectBean.ActItemsBean child = (CarBean.DataBean.EffectBean.ActItemsBean) getChild(groupPosition, childPosition);
        return child.getId();
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View groupView, ViewGroup parent) {

        GroupHolder gholder;
        if (groupView == null) {
            gholder = new GroupHolder();
            groupView = View.inflate(context, R.layout.item_shopcart_group, null);
            gholder.tv_group_name = (TextView) groupView.findViewById(R.id.tv_shop_name);
            gholder.rl_group = groupView.findViewById(R.id.group_rl);
            gholder.carMore = groupView.findViewById(R.id.car_more);
            groupView.setTag(gholder);
        } else {
            gholder = (GroupHolder) groupView.getTag();
        }
        final CarBean.DataBean.EffectBean.ActivityBean group = (CarBean.DataBean.EffectBean.ActivityBean) getGroup(groupPosition);
        if (group.getDefId() == 1) {//是活动的商品(N元选)
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) gholder.rl_group.getLayoutParams();
            params.height = ScreenTool.dpTopx(context, 37);
            gholder.rl_group.setLayoutParams(params);
            gholder.carMore.setVisibility(View.VISIBLE);
            int needNum = Integer.parseInt(group.getConditionNum());
            String title = "「N元购」";
            String format = "";
            if (group.getSelectNum() >= needNum) {//当选中的商品条目数大于等于活动需要的商品数量
                format = String.format(Locale.CHINA, "已满足%s元任选%2s件", group.getConditionPrice(), group.getConditionNum());
                gholder.tv_group_name.setTextColor(Color.parseColor("#1F453C"));
            } else {//当选中的商品条目数大于等于活动需要的商品数量
                int i = needNum - group.getSelectNum();
                format = String.format(Locale.CHINA, "再选%d件立享%s元另选%2s件", i,group.getConditionPrice(), group.getConditionNum());
                gholder.tv_group_name.setTextColor(Color.RED);
            }
            SpannableStringBuilder spannableStringBuilder = StringUtils.setColor(title + format);
            gholder.tv_group_name.setText(spannableStringBuilder);
        } else {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) gholder.rl_group.getLayoutParams();
            params.height = ScreenTool.dpTopx(context, 10);
            gholder.rl_group.setLayoutParams(params);
            gholder.carMore.setVisibility(View.GONE);
            gholder.tv_group_name.setText("");

        }
        //todo 满减活动
        //父条目设置tag
        groupView.setTag(R.id.group, groupPosition);//groupId;
        groupView.setTag(R.id.child, -1);//childId

        return groupView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View childView, ViewGroup parent) {

        final ChildHolder cholder;
        if (childView == null) {
            cholder = new ChildHolder();
            childView = View.inflate(context, R.layout.item_car_goods_item, null);
            cholder.check_box = (CheckBox) childView.findViewById(R.id.check_box);
            cholder.tv_Shixiao = (TextView) childView.findViewById(R.id.text_shixiao);
            cholder.iv_GoodImage = (ImageView) childView.findViewById(R.id.image_car_goods);
            cholder.tv_product_name = (TextView) childView.findViewById(R.id.car_good_name);
            cholder.tv_size = (TextView) childView.findViewById(R.id.car_goodnum);
            cholder.tv_price = childView.findViewById(R.id.car_goodprice);
            cholder.tv_increase = (TextView) childView.findViewById(R.id.car_good_add);
            cholder.tv_decrease = (TextView) childView.findViewById(R.id.car_good_cut);
            cholder.tv_count = (TextView) childView.findViewById(R.id.car_good_nums);
            cholder.tv_xiajia = (TextView) childView.findViewById(R.id.car_xiajia);
            cholder.tv_suilv = (TextView) childView.findViewById(R.id.car_suilv);
            cholder.carNumTextLl = childView.findViewById(R.id.car_numtext_ll);
            childView.setTag(cholder);
        } else {
            cholder = (ChildHolder) childView.getTag();
        }
        if (isLastChild) {
            childView.setBackgroundResource(R.drawable.shape_car_item_content);
        } else {
            childView.setBackgroundResource(R.drawable.shape_car_item_nomal);
        }
        final CarBean.DataBean.EffectBean.ActItemsBean product = (CarBean.DataBean.EffectBean.ActItemsBean) getChild(groupPosition, childPosition);

        if (product != null) {
            if (product.isShiXiao()) {//当他不等于空时就是 失效商品
                cholder.check_box.setVisibility(View.GONE);
                cholder.tv_Shixiao.setVisibility(View.VISIBLE);
                cholder.tv_xiajia.setVisibility(View.VISIBLE);
                cholder.carNumTextLl.setVisibility(View.GONE);
                if(product.isHideNumText()){
                    cholder.check_box.setVisibility(View.VISIBLE);
                    cholder.tv_Shixiao.setVisibility(View.GONE);
                }else {
                    cholder.check_box.setVisibility(View.GONE);
                    cholder.tv_Shixiao.setVisibility(View.VISIBLE);
                }
            } else {//当他等于空时就是 非失效商品
                cholder.check_box.setVisibility(View.VISIBLE);
                cholder.tv_Shixiao.setVisibility(View.GONE);
                cholder.tv_xiajia.setVisibility(View.GONE);
                if (product.isHideNumText()) {//编辑时需要隐藏数量加减按钮
                    cholder.carNumTextLl.setVisibility(View.GONE);
                } else {
                    cholder.carNumTextLl.setVisibility(View.VISIBLE);
                }
            }

            if (product.isChoosed()) {
                cholder.check_box.setChecked(true);
            } else {
                cholder.check_box.setChecked(false);
            }
            cholder.check_box.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    product.setChoosed(((CheckBox) v).isChecked());
                    cholder.check_box.setChecked(((CheckBox) v).isChecked());
                    checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());// 暴露子选接口
                }
            });
            cholder.tv_increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count, cholder.check_box.isChecked());// 暴露增加接口
                }
            });
            cholder.tv_decrease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count, cholder.check_box.isChecked());// 暴露删减接口
                }
            });

        }
        //设置件数

        cholder.tv_count.setText(String.valueOf(product.getNumber()));

        //设置title

        IconTextSpanUtils.setText(context, product.getTitle(), product.getSellpattern(), cholder.tv_product_name);

        //设置图片

        Glide.with(context).load(Gjmall.getOssHost() + product.getThumbImageUrl()).placeholder((R.mipmap.xiangqing)).into(cholder.iv_GoodImage);

        //sku规格

        cholder.tv_size.setText(String.valueOf(product.getSkuname()));

        //税率
        if (product.getTaxation() != 0.00) {
            cholder.tv_suilv.setVisibility(View.VISIBLE);
            cholder.tv_suilv.setText("税率约:" + String.valueOf(product.getTaxation()));
        } else {
            cholder.tv_suilv.setVisibility(View.GONE);
        }
        //价格
        if (product.isWhetherActivite()) {//商品是抢购商品
            if(System.currentTimeMillis() > product.getGmtStart() && System.currentTimeMillis() <product.getGmtStop()) {
                String s = PriceFormatUtils.valueFormatWithTwo(String.valueOf(product.getSnapupPrice()));
                cholder.tv_price.setText("¥" + s);
            }else {
                String s = PriceFormatUtils.valueFormatWithTwo(String.valueOf(product.getPrice()));
                cholder.tv_price.setText("¥" + s);
            }
        } else {//商品不是抢购商品
            String s = PriceFormatUtils.valueFormatWithTwo(String.valueOf(product.getPrice()));
            cholder.tv_price.setText("¥" + s);
        }

        //子条目设置tag
        childView.setTag(R.id.group, groupPosition);
        childView.setTag(R.id.child, childPosition);
        return childView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 组元素绑定器
     */
    private class GroupHolder {
        TextView tv_group_name;
        RelativeLayout rl_group;
        ImageView carMore;
    }

    /**
     * 子元素绑定器
     */
    private class ChildHolder {
        CheckBox check_box;//是否选中条目的按钮
        TextView tv_Shixiao;//商品失效的文字
        ImageView iv_GoodImage;//商品的图片
        TextView tv_product_name;//商品名字
        TextView tv_size;//商品的大小
        TextView tv_price;//商品的价格
        TextView tv_increase;//加号按钮
        TextView tv_decrease;//减号按钮
        TextView tv_count;//选中商品的数量
        TextView tv_xiajia;//下架商品提示
        TextView tv_suilv;//税率
        LinearLayout carNumTextLl;//数量加减布局
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素位置
         * @param isChecked     组元素选中与否
         */
        public void checkGroup(int groupPosition, boolean isChecked);

        /**
         * 子选框状态改变时触发的事件
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param isChecked     子元素选中与否
         */
        public void checkChild(int groupPosition, int childPosition, boolean isChecked);
    }

    /**
     * 改变数量的接口
     */
    public interface ModifyCountInterface {
        /**
         * 增加操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
    }


}
